package com.aaa.docter.service.Impl;

import com.aaa.docter.Fegin.UserFeign;
import com.aaa.docter.dao.CareHistoryDao;
import com.aaa.docter.dao.CareOrderDao;
import com.aaa.docter.dao.CareOrderItemDao;
import com.aaa.docter.dao.SchedulingDao;
import com.aaa.docter.service.CareHistoryService;
import com.aaa.docter.vo.HosVo;
import com.aaa.docter.vo.SchedulingVo;
import com.aaa.entity.*;
import com.aaa.util.IdWorker;
import com.aaa.util.JwtUtil;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class CareHistoryServiceImpl implements CareHistoryService {
    @Autowired
    private CareHistoryDao careHistoryDao;

    @Autowired
    private CareOrderDao careOrderDao;

    @Autowired
    private CareOrderItemDao careOrderItemDao;

    @Autowired
    private UserFeign userFeign;

    //    患者库档案信息查询
    @Override
    public Result<List<CareHistory>> getAll(String patientId) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(patientId)) {
            wrapper.eq("patient_id", patientId);
            List<CareHistory> careHistories = careHistoryDao.selectList(wrapper);
            for (CareHistory careHistory : careHistories) {
                QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
                if (Objects.nonNull(careHistory.getChId())) {
                    wrapper1.eq("ch_id", careHistory.getChId());
                    List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
                    careHistory.setCareOrderList(careOrders);
                    for (CareOrder careOrder : careOrders) {
                        QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                        if (Objects.nonNull(careOrder.getCoId())) {
                            wrapper2.eq("co_id", careOrder.getCoId());
                            List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper2);
                            careOrder.setCareOrderItemList(careOrderItems);
                        }
                    }
                }
            }
//        return careHistories;
            System.out.println("careHistories = " + careHistories);
            return new Result<>(2000, "查询患者病历信息成功", careHistories);
        }
        return null;
    }

    ////    根据挂号单ID查询对应的病例信息
    @Override
    public Result<List<CareHistory>> getCareHistoryByRegId(String registrationId) {

      List<CareHistory> careHistories= careHistoryDao.getCareHistoryByRegId(registrationId);
        System.out.println("careHistories查询病例信息============== = " + careHistories);
        return new Result<>(2000, "查询病例成功", careHistories);
    }

    //    保存病例
    @Override
    public Result handleSaveCareHistory(String registrationId, CareHistory careHistory) {

        //添加CH,雪花算法
        IdWorker worker = new IdWorker();//雪花算法
        long chId = worker.nextId();
        String str = "CH" + chId;
//        获取token
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
//        System.out.println("token获取======================== = " + token);
        Map<String, Object> tokenInfo = JwtUtil.getTokenChaim(token);
//        在token里获取当前用户手机号信息
        Long phone = Long.valueOf(tokenInfo.get("username").toString());
        System.out.println("phone 000000000000000000000= " + phone);
//        获取手机号信息
        User user = userFeign.queryUserByPhone(phone);
        System.out.println("user ========= " + user);
        careHistory.setChId(str);
        careHistory.setUserId(user.getUserId());
        careHistory.setUserName(user.getUserName());
        careHistory.setDeptId(user.getDeptId());
        Long userId = user.getDeptId();
        Dept deptById = userFeign.getDeptById(userId);
        System.out.println("deptById =========== " + deptById);
        careHistory.setDeptName(String.valueOf(deptById.getDeptName()));
        careHistory.setRegId(registrationId);
        careHistory.setCareTime(new Date());
        int insert = careHistoryDao.insert(careHistory);
        if (insert > 0) {
//            return Result.success().setData(chId);
            return new Result(2000,"ok",str);
        }
        return Result.error();

    }

}
